package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiyun.pojo.DcDriver;
import com.jiyun.service.LoginService;
import com.jiyun.utils.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/login")
public class LoginController {

    // 定义Redis中存储验证码的键的前缀常量
    private static final String REDIS_VERIFY_CODE_PREFIX = "MY:";

    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 生成随机数方法
    public static String generateRandomString(int length) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();

        // 定义数字和字母的字符范围
        String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            stringBuilder.append(characters.charAt(index));
        }

        return stringBuilder.toString();
    }

    // 从Redis获取验证码并验证的方法
    private boolean verifyCaptchaFromRedis(String tel, String verify) {
        try {
            String captcha = (String) redisTemplate.opsForValue().get(REDIS_VERIFY_CODE_PREFIX + tel);
            if (captcha == null) {
                return false;
            }
            return captcha.equals(verify);
        } catch (Exception e) {
            // 这里可以根据具体需求记录日志或者进行其他错误处理
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/zhuce")
    public Result save(@RequestBody DcDriver dcDriver) {

        // 先判断手机号是否已存在
        LambdaQueryWrapper<DcDriver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DcDriver::getTel, dcDriver.getTel());
        DcDriver dcDriver2 = loginService.getOne(queryWrapper);
        if (dcDriver2 != null) {
            return Result.fail();
        }

        DcDriver dcDriver1 = new DcDriver();
        dcDriver1.setName(dcDriver.getName());
        dcDriver1.setSex(dcDriver.getSex());
        dcDriver1.setTel(dcDriver.getTel());
        dcDriver1.setBirthday(dcDriver.getBirthday());
        dcDriver1.setMailbox(dcDriver.getMailbox());
        dcDriver1.setVerify(dcDriver.getVerify());
        dcDriver1.setRegistrationTime(new Date());//注册时间
        dcDriver1.setAdministrativeDistrict("北京");
        dcDriver1.setCity("北京");
        dcDriver1.setLoginTime(new Date());
        dcDriver1.setPhoto("https://oss-jiudianguanli.oss-cn-beijing.aliyuncs.com/20220307144348_fdfea.jpg");

        // 保存用户
        loginService.save(dcDriver1);

        // 返回正确信息
        return Result.ok();
    }

    @PostMapping("/tologin")
    public Result tologin(String tel, String verify) {

        // 先判断手机号是否存在
        LambdaQueryWrapper<DcDriver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DcDriver::getTel, tel);
        DcDriver dcDriver = loginService.getOne(queryWrapper);
        if (dcDriver == null) {
            return Result.fail();
        }

        // 从Redis获取验证码并验证
        if (!verifyCaptchaFromRedis(tel, verify)) {
            return Result.fail();
        }

        return Result.ok(dcDriver);
    }
}